﻿//JQueryUI presetup
$(window).bind('load', function() {        
    $(".mediaSelect li").draggable({ helper: 'clone' });
    $(".thumbMediaSelect li").draggable({ helper: 'clone' });

    $('#actionPanel-broadcast').droppable({
        accept: '.thumbMediaSelect li, .mediaSelect li',
        drop: function(ev, ui) {
            var $that = $(this);
            $that.find('ul').append(ui.draggable);
            //$that.append(ui.draggable);
            setTimeout(function() { $('.hosts').trigger('repaint') }, 1);
            ui.draggable.parent().fadeOut('fast', function() {
                ui.draggable
                           .hide()
                            .fadeIn('slow');
                ui.draggable.parent().fadeIn('fast');
                addToBroadcast(ui);
            });
        }
    });

    $('#actionPanel-shows').droppable({
        accept: '.show',
        drop: function(ev, ui) {
            var $that = $(this);
            $that.find('ul').append(ui.draggable);
            setTimeout(function() { $('.hosts').trigger('repaint') }, 1);
            ui.draggable.parent().fadeOut('fast', function() {
                ui.draggable
                           .hide()
                            .fadeIn('slow');
                ui.draggable.parent().fadeIn('fast');
                removeFromBroadcast(ui);
            });
        }
    });

    $('#actionPanel-music').droppable({
        accept: '.music',
        drop: function(ev, ui) {
            var $that = $(this);
            $that.find('ul').append(ui.draggable);
            setTimeout(function() { $('.hosts').trigger('repaint') }, 1);
            ui.draggable.parent().fadeOut('fast', function() {
                ui.draggable
                           .hide()
                            .fadeIn('slow');
                ui.draggable.parent().fadeIn('fast');
                removeFromBroadcast(ui);
            });
        }
    });

    $('#actionPanel-ads').droppable({
        accept: '.ad',
        drop: function(ev, ui) {
            var $that = $(this);
            $that.find('ul').append(ui.draggable);
            setTimeout(function() { $('.hosts').trigger('repaint') }, 1);
            ui.draggable.parent().fadeOut('fast', function() {
                ui.draggable
                           .hide()
                            .fadeIn('slow');
                ui.draggable.parent().fadeIn('fast');
                removeFromBroadcast(ui);
            });
        }

    });

    $('a.prefInc').livequery('click', function() {
        var $this = $(this);
        var $item = $this.parent().parents('li');
        
        $item.fadeOut('fast', function() {
        incPreference($item);
            $item.fadeIn('fast');
        });
        return false;
    });

    $('a.prefDec').livequery('click', function() {
        var $this = $(this);
        var $item = $this.parent().parents('li');

        $item.fadeOut('fast', function() {
            decPreference($item);
            $item.fadeIn('fast');
        });
        return false;
    });

});

//var broadcastItems[] = new Array();


function addToBroadcast(ui) {
    id = ui.draggable.attr("id");
    pref = ui.draggable.find('strong').text();
    addItemToBroadcast(id, pref);}

function addItemToBroadcast(id, pref) {
    currentValues = $('#broadcastCollection').attr("value");
    $('#broadcastCollection').attr({ value: currentValues + ";" + id + "(" + pref + ")" });}

function removeFromBroadcast(ui) {
    id = ui.draggable.attr("id");
    pref = ui.draggable.find('strong').text();
    removeItemFromBroadcast(id, pref);}

function removeItemFromBroadcast(id, pref) {
    currentValues = $('#broadcastCollection').attr("value");
    sub = id + "(" + pref + ")";
    modifiedValues = currentValues;
    modifiedValues = remove(currentValues, sub);
    $('#broadcastCollection').attr({ value: modifiedValues });}

function incPreference(ui) {
    currentPref = parseInt(ui.find('strong').text());
    if (currentPref == 3) return;

    ui.find('strong').text(currentPref + 1);
    setPreference(ui.attr("id"), currentPref, currentPref + 1);}

function decPreference(ui) {
    currentPref = parseInt(ui.find('strong').text());
    if (currentPref == 1) return;

    ui.find('strong').text((currentPref - 1));
    setPreference(ui.attr("id"), currentPref, currentPref - 1);}

function setPreference(id, oldpref, newpref) {
    removeItemFromBroadcast(id, oldpref);
    addItemToBroadcast(id, newpref);}
    

function remove(s, t) {
    i = s.indexOf(t);
    r = "";
    if (i == -1) return s;
    r += s.substring(0, i) + remove(s.substring(i + t.length), t);
    return r;}

